
package com.sdk.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "gamebox.db";

    public static final int DATABASE_VERSION = 1;

    public static final String TABLE_NAME = "gamebox";

    private static final String COLUMN_CATEGORY = "category";

    private static final String COLUMN_NAME = "name";

    private static final String COLUMN_GAMELINK = "game_link";

    private static final String COLUMN_GAME_PIC_NAME = "game_pic_name";

    private static final String TAG = DatabaseHelper.class.getSimpleName();

    private static DatabaseHelper mDatabaseHelper;

    private static byte[] SYNC = new byte[0];

    private String mPath;

    private SQLiteDatabase mDatabase;

    private Context mContext;

    private DatabaseHelper(Context c) {
        this(c, DATABASE_NAME, null, DATABASE_VERSION);

    }

    private DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
        mPath = context.getDatabasePath(DATABASE_NAME).getPath();
        initializeDatabase();
        mDatabase = getWritableDatabase();
    }

    public synchronized static DatabaseHelper getInstance(Context c) {
        if (null == mDatabaseHelper) {
            mDatabaseHelper = new DatabaseHelper(c);
        }
        return mDatabaseHelper;
    }

    /**
     * When the application started, make sure to copy database file to local
     * file system
     */
    private void initializeDatabase() {
        synchronized (SYNC) {
            File file = new File(mPath);
            file.getParentFile().mkdirs();
            if (!file.exists()) {
                try {
                    Log.i(TAG, "initialize database file.");
                    int bufferSize = 4096;
                    byte[] buffer = new byte[bufferSize];
                    InputStream stream = mContext.getResources().openRawResource(getDBResource());
                    FileOutputStream outputStream = new FileOutputStream(file);
                    while (-1 != stream.read(buffer)) {
                        outputStream.write(buffer);
                    }
                } catch (IOException e) {
                    Log.e(TAG, "eror when copy database file.");
                }
            } else {
                Log.d(TAG, " [initializeDatabase] the database is already exist.");
            }
            SYNC.notifyAll();
        }
    }
    public int getDBResource(){
        return 0;
    }

    @Override
    public void close() {
        if (null != mDatabase && !mDatabase.isOpen()) {
            mDatabase.close();
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "[onCreate] the database is create.");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXIST " + DATABASE_NAME);
    }

    // public List<String> getAllCategory() {
    // List<String> categoryList = new ArrayList<String>();
    // if (mDatabase == null) {
    // Log.e(TAG, " [getAllCategory] " +
    // " SQLiteDatabase is null,please check it.");
    // }
    // mDatabase.query(TABLE_NAME, new String[] {
    // COLUMN_CATEGORY
    // }, selection, null, null, null, null);
    // return categoryList;
    // }
}
